package com.codehub.jadmin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

import com.codehub.jadmin.domain.TestUser;
import com.codehub.jadmin.service.HelloWorldService;

@Api(value="TestController",tags="测试接口")
@RestController
@RequestMapping("/test")
public class TestController {
	
	@Resource
	private HelloWorldService helloWorldService;
	
	@ApiOperation(value="hello接口", notes="hello world接口")
	@GetMapping("/hello")
	public String hello(String name) {
		return helloWorldService.getHelloMessage(name);
	}
	
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", required = true)
	})
	@RequestMapping(value = "/user",method = RequestMethod.POST)
	public TestUser create(@RequestBody TestUser user){	
		System.out.println("user:"+user);
		return user;
	}
	
	@ApiOperation(value="修改用户", notes="根据id来修改用户")
	@ApiImplicitParams({
		@ApiImplicitParam(dataType = "java.lang.Long", name = "id", value = "id", required = true, paramType = "path"),
		@ApiImplicitParam(dataType = "User", name = "user", value = "用户信息", required = true)
	})
	@RequestMapping(value = "/user/{id}",method = RequestMethod.PUT)
	public TestUser update(@PathVariable Long id, @RequestBody TestUser user){
		System.out.println("id:"+id+", user:"+user);
		user.setId(id);
		return user;
	}

	@ApiOperation(value="获取指定id用户详细信息", notes="根据user的id来获取用户详细信息")
	@ApiImplicitParam(name = "id",value = "用户id", dataType = "String", paramType = "path")
	@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
	public TestUser getUser(@PathVariable Long id){
	
		TestUser user = new TestUser();
	    user.setId(id);
	    user.setPassword("abc");
	    user.setUsername("12345");
	    return user;
	}

	@ApiOperation(value="获取所有用户详细信息", notes="获取用户列表信息")
	@RequestMapping(value = "/users", method = RequestMethod.GET)
	public List<TestUser> getUserList(){
	
	    List<TestUser> list = new ArrayList<>();
	    TestUser user = new TestUser();
	    user.setId(15L);
	    user.setPassword("ricky");
	    user.setUsername("root");
	
	    list.add(user);
	
	    return list;
	}

	@ApiOperation(value="删除指定id用户", notes="根据id来删除用户信息")
	@ApiImplicitParam(name = "id",value = "用户id", dataType = "java.lang.Long", paramType = "path")
	@RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Long id){
	    System.out.println("delete user:"+id);
	    return "OK";
	}
}
